package gui;

import javax.swing.*;
import java.awt.event.*;

import domein.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.DefaultListModel;

/**
 *
 * @author Stijn
 */
public class InstructorWindow extends javax.swing.JDialog {

    int m_InstructorID;
    int m_WebID;
    int aantalToeschouwers;
    WebinarController m_Controller;
    ListCreator m_Creator;
    private ArrayList<String> listChat1;
    private Timer timer;
    DefaultListModel deelnemers;
    ArrayList qaList;
    DefaultListModel listQA;

    /** Creates new form InstructorWindow */
    public InstructorWindow(java.awt.Frame parent, int instructerID, Webinar web) {
        super(parent);
        setTitle("Instructeur \t" + web.getOnderwerp());

        m_InstructorID = instructerID;
        m_WebID = web.getWebinarID();
        m_Controller = WebinarController.getInstance();
        listChat1 = m_Controller.geefChatWebinar(m_WebID);

        m_Creator = new ListCreator();

        deelnemers = new DefaultListModel();
        for (Gebruiker g : web.getInschrijvingen().values()) {
            deelnemers.addElement(g.getFamilienaam() + " " + g.getVoornaam());
            aantalToeschouwers += 1;
        }

        listQA = new DefaultListModel();
        for (Object o : m_Controller.getQAList(m_WebID)) {
            listQA.addElement(o);
        }
        initComponents();

        for (String s : listChat1) {
            listChat.append("\n" + s);
        }

        ReloadQA();
        setKickEnabled();
        listQAClickHandler();

        ActionListener taskPerformer = new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                ArrayList<String> listChat2 = m_Controller.geefChatWebinar(m_WebID);

                if (!listChat1.equals(listChat2)) {
                    listChat1 = listChat2;
                    String s = listChat1.get(listChat1.size() - 1);
                    listChat.append("\n" + s);
                    scrollChat.getVerticalScrollBar().setValue(scrollChat.getVerticalScrollBar().getMaximum());
                }

                DefaultListModel listQA2 = new DefaultListModel();
                for (Object o : m_Controller.getQAList(m_WebID)) {
                    listQA2.addElement(o);
                }
                if (!listQA.equals(listQA2)) {
                    listQA = listQA2;
                    ListQA.setModel(listQA);
                }
                timer.restart();

            }
        };

        timer = new Timer(400, taskPerformer);
        timer.start();
        //scherminstellingen:
        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension screenSize = tk.getScreenSize();
        int screenWidth = screenSize.width;

        setLocation((screenWidth - 857) / 2, 0);
        setSize(857, 540);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        btnAfsluiten = new javax.swing.JButton();
        scrollQA = new javax.swing.JScrollPane();
        ListQA = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jlistDeelnemers = new javax.swing.JList();
        btnKick = new javax.swing.JButton();
        scrollChat = new javax.swing.JScrollPane();
        listChat = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jPanel3.setMaximumSize(new java.awt.Dimension(857, 32767));
        jPanel3.setPreferredSize(new java.awt.Dimension(540, 857));

        btnAfsluiten.setText("Afsluiten");
        btnAfsluiten.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAfsluitenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(723, Short.MAX_VALUE)
                .addComponent(btnAfsluiten)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAfsluiten)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAfsluiten.getAccessibleContext().setAccessibleName("btnClose");

        ListQA.setModel(listQA);
        ListQA.setAutoscrolls(false);
        ListQA.setCellRenderer(new QACellRenderer());
        ListQA.setName("ListQA"); // NOI18N
        ListQA.setOpaque(false);
        scrollQA.setViewportView(ListQA);

        jLabel1.setText("Aantal Toeschouwers: "+aantalToeschouwers);

        jlistDeelnemers.setModel(deelnemers);
        jlistDeelnemers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jlistDeelnemers);
        jlistDeelnemers.getAccessibleContext().setAccessibleName("ListViewers");

        btnKick.setText("Kick");
        btnKick.setEnabled(false);
        btnKick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKickActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jLabel1))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnKick)
                        .addGap(64, 64, 64))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnKick)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        jLabel1.getAccessibleContext().setAccessibleName("lblNr");
        btnKick.getAccessibleContext().setAccessibleName("btnKick");

        listChat.setColumns(20);
        listChat.setEditable(false);
        listChat.setLineWrap(true);
        listChat.setRows(5);
        listChat.setWrapStyleWord(true);
        scrollChat.setViewportView(listChat);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(scrollQA, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(scrollChat, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollChat, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                    .addComponent(scrollQA, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel1.getAccessibleContext().setAccessibleName("pnlViewers");
    }// </editor-fold>//GEN-END:initComponents

    private void btnAfsluitenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAfsluitenActionPerformed
        m_Controller.stopWebinar(m_WebID);
        setVisible(false);
        dispose();
    }//GEN-LAST:event_btnAfsluitenActionPerformed

    private void btnKickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKickActionPerformed
    }//GEN-LAST:event_btnKickActionPerformed

    private void ReloadQA() {
        ListQA.setModel(m_Creator.GetList());
        //maak dummy class for refractor and backgroundset
    }

    private void setKickEnabled() {
        jlistDeelnemers.setSelectedIndex(0);

        MouseListener mouseListener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = jlistDeelnemers.locationToIndex(e.getPoint());
                    System.out.println("Double clicked on Item " + index);
                }
            }
        };
        jlistDeelnemers.addMouseListener(mouseListener);

    }

    @SuppressWarnings("empty-statement")
    private void listQAClickHandler() {

        MouseListener mouseListener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    try {
                        Vraag v = (Vraag) ListQA.getSelectedValue();
                        String m_Answer = "";

                        String previousanswer = m_Controller.GetAnswer(m_WebID, v.getVraagID()).getTekst();

                        try {
                            m_Answer = JOptionPane.showInputDialog(null, v.getTekst(), "Webinar vraag - antwoord", 1);
                            if (!m_Answer.equals("")) {
                                if (previousanswer == null) {
                                    m_Controller.SaveAnswer(v.getVraagID(), m_InstructorID, m_WebID, m_Answer);
                                } else {
                                    m_Controller.updateAnswer(v.getVraagID(), m_WebID, m_Answer);
                                }
                            }

                        } catch (NullPointerException ex) {
                        }
                        timer.restart();
                    } catch (ClassCastException ex) {
                    }
                }
            }
        ;


        };

     ListQA.addMouseListener(mouseListener);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ListQA;
    private javax.swing.JButton btnAfsluiten;
    private javax.swing.JButton btnKick;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList jlistDeelnemers;
    private javax.swing.JTextArea listChat;
    private javax.swing.JScrollPane scrollChat;
    private javax.swing.JScrollPane scrollQA;
    // End of variables declaration//GEN-END:variables
}
